package com.sgb.kjwl.viewmodel.enterprise;

import android.util.Log;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.AttentionEntity;
import com.sgb.kjwl.model.entity.enterprise.CompanyDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.CopyrightEntity;
import com.sgb.kjwl.model.entity.enterprise.PatentDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.PropertyDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.TrademarkEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverDetailActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MakeOverDetailModel extends BaseViewModel {

    private MakeOverDetailActivity makeOverDetailActivity;
    public CopyrightEntity copyrightEntity;
    public TrademarkEntity trademarkEntity;
    public PatentDetailEntity patentDetailEntity;

    public MakeOverDetailModel(MakeOverDetailActivity makeOverDetailActivity) {
        this.makeOverDetailActivity = makeOverDetailActivity;
    }

    public void trademarkDetail(long id) {
        makeOverDetailActivity.loading();
        DialogHelper.showProgressDialog(makeOverDetailActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().queryTrademarkDetail(id).enqueue(new Callback<BaseEntity<TrademarkEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TrademarkEntity>> call, Response<BaseEntity<TrademarkEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    trademarkEntity = response.body().getData();
                    makeOverDetailActivity.getTrademarkEntity(trademarkEntity);
                    makeOverDetailActivity.success();
                } else {
                    MToast.showToast(makeOverDetailActivity.getApplicationContext(), response.body().getMsg());
                    makeOverDetailActivity.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TrademarkEntity>> call, Throwable t) {
                Log.e("tmessage",t.getMessage());
                DialogHelper.dismissProgressDialog();
                makeOverDetailActivity.error();
            }
        });
//        getCommonQuestion("1");
    }

    public void copyrightDetail(long id) {
        makeOverDetailActivity.loading();
        DialogHelper.showProgressDialog(makeOverDetailActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().queryCopyrightDetail(id).enqueue(new Callback<BaseEntity<CopyrightEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<CopyrightEntity>> call, Response<BaseEntity<CopyrightEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    copyrightEntity = response.body().getData();
                    makeOverDetailActivity.getCopyrightEntity(copyrightEntity);
                    makeOverDetailActivity.success();
                } else {
                    MToast.showToast(makeOverDetailActivity.getApplicationContext(), response.body().getMsg());
                    makeOverDetailActivity.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CopyrightEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                makeOverDetailActivity.error();
            }
        });
//        getCommonQuestion("2");
    }

    public void patentDetail(long id) {
        makeOverDetailActivity.loading();
        DialogHelper.showProgressDialog(makeOverDetailActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().queryPatentDetail(id).enqueue(new Callback<BaseEntity<PatentDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PatentDetailEntity>> call, Response<BaseEntity<PatentDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    patentDetailEntity = response.body().getData();
                    makeOverDetailActivity.getPatentEntity(patentDetailEntity);
                    makeOverDetailActivity.success();
                } else {
                    MToast.showToast(makeOverDetailActivity.getApplicationContext(), response.body().getMsg());
                    makeOverDetailActivity.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PatentDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                makeOverDetailActivity.error();
            }
        });
//        getCommonQuestion("3");
    }

    public void companyDetail(long id) {
        makeOverDetailActivity.loading();
        DialogHelper.showProgressDialog(makeOverDetailActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().queryCompanyDetail(id).enqueue(new Callback<BaseEntity<CompanyDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<CompanyDetailEntity>> call, Response<BaseEntity<CompanyDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverDetailActivity.getCompanyDetail(response.body().getData());
                    makeOverDetailActivity.success();
                } else {
                    MToast.showToast(makeOverDetailActivity.getApplicationContext(), response.body().getMsg());
                    makeOverDetailActivity.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CompanyDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                makeOverDetailActivity.error();
            }
        });
//        getCommonQuestion("5");
    }

    public void propertyDetail(long id) {
        makeOverDetailActivity.loading();
        DialogHelper.showProgressDialog(makeOverDetailActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().queryPropertyDetail(id).enqueue(new Callback<BaseEntity<PropertyDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PropertyDetailEntity>> call, Response<BaseEntity<PropertyDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverDetailActivity.getPropertyDetail(response.body().getData());
                    makeOverDetailActivity.success();
                } else {
                    MToast.showToast(makeOverDetailActivity.getApplicationContext(), response.body().getMsg());
                    makeOverDetailActivity.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PropertyDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                makeOverDetailActivity.error();
            }
        });
//        getCommonQuestion("4");
    }

    public void focusMakeOver(Map<String, Object> map, int isGz) {
        DialogHelper.showProgressDialog(makeOverDetailActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getEnterpriseAPI().saveAttention(map).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData().contains("下架")) {
                        MToast.showToast(makeOverDetailActivity, response.body().getData());
                    } else {
                        makeOverDetailActivity.setTvIsGz(isGz);
                        makeOverDetailActivity.setDataId(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getAttentionStatus(String userNo, int type, String dataId) {
        DialogHelper.showProgressDialog(makeOverDetailActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getEnterpriseAPI().getAttentionInfoByData(userNo, type, dataId).enqueue(new Callback<BaseEntity<List<AttentionEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AttentionEntity>>> call, Response<BaseEntity<List<AttentionEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {//已关注
                        makeOverDetailActivity.setTvIsGz(0);
                        makeOverDetailActivity.setDataId(String.valueOf(response.body().getData().get(0).getId()));
                    } else {
                        makeOverDetailActivity.setTvIsGz(1);
                    }

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AttentionEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 推荐商标列表
     *
     * @param category
     * @param id
     */
    public void getRecommendList(String category, String id) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getMakerRecommendlist(category, id).enqueue(new Callback<BaseEntity<List>>() {
            @Override
            public void onResponse(Call<BaseEntity<List>> call, Response<BaseEntity<List>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverDetailActivity.showRecommend(response.body().getData());
                } else {
                    MToast.showToast(makeOverDetailActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List>> call, Throwable t) {
            }
        });
    }

    /**
     * 推荐版权列表
     *
     * @param category
     * @param id
     */
    public void getCopyrightRecommendList(String category, String id) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getCopyrightrRecommendlist(category, id).enqueue(new Callback<BaseEntity<List>>() {
            @Override
            public void onResponse(Call<BaseEntity<List>> call, Response<BaseEntity<List>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverDetailActivity.showCopyrightRecommend(response.body().getData());
                } else {
                    MToast.showToast(makeOverDetailActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List>> call, Throwable t) {
            }
        });
    }

    /**
     * 推荐专利列表
     *
     * @param category
     * @param id
     */
    public void getPantentRecommendList(String category, String id) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPatentRecommendlist(category, id).enqueue(new Callback<BaseEntity<List>>() {
            @Override
            public void onResponse(Call<BaseEntity<List>> call, Response<BaseEntity<List>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverDetailActivity.showPatentRecommend(response.body().getData());
                } else {
                    MToast.showToast(makeOverDetailActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 推荐公司列表
     *
     * @param category
     * @param id
     */
    public void getCompanyRecommendList(String category, String id) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getCompanyRecommendlist(category, id).enqueue(new Callback<BaseEntity<List>>() {
            @Override
            public void onResponse(Call<BaseEntity<List>> call, Response<BaseEntity<List>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverDetailActivity.showCompanyRecommend(response.body().getData());
                } else {
                    MToast.showToast(makeOverDetailActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List>> call, Throwable t) {
            }
        });
    }

    /**
     * 推荐资产列表
     *
     * @param category
     * @param id
     */
    public void getPropertyRecommendList(String category, String id) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPropertyRecommendlist(category, id).enqueue(new Callback<BaseEntity<List>>() {
            @Override
            public void onResponse(Call<BaseEntity<List>> call, Response<BaseEntity<List>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverDetailActivity.showPropertyRecommend(response.body().getData());
                } else {
                    MToast.showToast(makeOverDetailActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List>> call, Throwable t) {
            }
        });
    }

    /**
     * 判断能否购买
     */
    public void gotocreateorder(int orderType,String id) {

        NewAPIManager.getInstance().getEnterPrisenewAPI().getMakeOverType(orderType, id,UserOperating.getInstance().isCompany()||UserOperating.getInstance().isTeam()? SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,""):SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO,""), UserOperating.getInstance().isCompany()?1:UserOperating.getInstance().isTeam()?2:0).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null ) {
                    return;
                }
                if (response.body().getData() != null&&response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String state = response.body().getData()==null?"":response.body().getData()+"";
                    if("上架".equals(state)) {
                        makeOverDetailActivity.gotobuy();
                    }else{
                        makeOverDetailActivity.getDetail();
                    }
                } else {
                    MToast.showToast(makeOverDetailActivity, response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(makeOverDetailActivity,t.getMessage());
            }
        });
    }
}
